<script>
	export default {
		name: "goods-item"
	}
</script>
<script setup>
	import {
		computed,
		ref
	} from 'vue'
	const props = defineProps({
		goods: Object,
		withRadio: {
			type: Boolean,
			default: false
		},
		withCounter: {
			type: Boolean,
			default: false
		},
	})
	const defaultImg = ref(
		"https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"
	)
	const fixedPrice = computed(() => (+props.goods.goods_price).toFixed(2))
	// 处理点击radio事件
	const emit = defineEmits(['radioChange', 'cntChange'])
	const dealRadioClick = () => {
		emit('radioChange', {
			goods_id: props.goods.goods_id,
			goods_state: !props.goods.goods_state
		})
	}
	// 处理数量变化事件
	const dealCntChange = e => {
		emit('cntChange', {
			goods_id: props.goods.goods_id,
			goods_count: +e
		})
	}
</script>
<template>
	<view class="goods-item">
		<view class="goods-item-left">
			<radio :checked="props.goods.goods_state" color="#C00000" @click="dealRadioClick" v-if="props.withRadio">
			</radio>
			<image :src="props.goods.goods_small_logo||defaultImg" class="goods-small-pic"></image>
		</view>
		<view class="goods-item-right">
			<view class="goods-name">
				{{props.goods.goods_name}}
			</view>
			<view class="goods-info">
				<view class="goods-info-price">
					￥{{fixedPrice}}
				</view>
				<view v-if="props.withCounter">
					<uni-number-box :min="1" :value="props.goods.goods_count" @change="dealCntChange"></uni-number-box>
				</view>
			</view>
		</view>
	</view>
</template>


<style lang="scss">
	.goods-item {
		display: flex;
		padding: 10px;
		border-bottom: 1px solid #f0f0f0;
		background-color: #fff;

		.goods-item-left {
			margin-right: 10px;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-small-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			flex: 1;

			.goods-name {
				font-size: 14px;
			}

			.goods-info {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.goods-info-price {
					color: #C00000;
					font-size: 17px;
				}
			}
		}
	}
</style>